package com.ckzp.jfinal.system.controller;

import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.system.cache.SysTypeCache;
import com.ckzp.jfinal.system.model.*;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2019/10/25.
 */
public class SysTypeController extends BaseController {

    public void index() {
        renderText("系统分类管理");
    }

    /**
     * 分类列表
     */
    @Roles(roles = "system")
    public void SystemTypeList() {
        render("SystemTypeList.html");
    }

    /**
     * 分类列表
     */
    @Roles(roles = "system")
    public void AddSystemType() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("type", new SysType());
        setAttr("form", fd);
        render("ModifySystemType.html");
    }

    /**
     * 分类列表
     */
    @Roles(roles = "system")
    public void EditSystemType() {
        String type_code = getPara("type_code", "");
        SysType sr = SysType.dao.findById(type_code);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("form", fd);
        setAttr("type", sr);
        render("ModifySystemType.html");
    }


    /**
     * 分类明细列表
     */
    @Roles(roles = "system")
    public void SystemTypeDetailList() {
        String type_code = getPara("type_code");
        FormData fd = FormData.dao().setKey(type_code);
        setAttr("form", fd);
        render("SystemTypeDetailList.html");
    }


    /**
     * 系统主分类弄表API
     */
    @Roles(roles = "system")
    public void SystemTypeDataApi() {
        //////////////////
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_type");
        Page<SysType> pages = ps.paginate(SysType.dao);

        List datalist = pages.getList();
//        for (int k = 0; k < datalist.size(); k++) {
//            SystemRole st = (SystemRole) datalist.get(k);
//        }
        LayuiData ld = LayuiData.newData();
        ld.setCount(pages.getTotalRow());
        ld.setData(datalist);
        renderJson(ld);
    }


    /**
     * 取得明细列表API
     */
    @Roles(roles = "system")
    public void SystemTypeDetailDataApi() {
        //////////////////

        PageSearch ps = PageSearch.n()
                .setTable("sys_type_detail")
                .search("type_code", getPara("type_code"))
                .initSearch(getRequest());
        Page<SysTypeDetail> pages = ps.paginate(SysTypeDetail.dao);
        //////////////
        int page = getParaToInt("page", 1);
        int limit = getParaToInt("limit", 10);
        String type_code = getPara("type_code");

//        String sql = "from sys_type_detail where type_code=? ";
//        sql = addSearch(sql, "role_id", "=", "string", getPara("searchkey[type_code]"));
//        sql = addSearch(sql, "role_name", "like", "string", getPara("searchkey[rolename]"));
//        if (DEBUG) {
//            System.out.println("search_sql==" + sql);
//        }
//        Page<SystemTypeDetail> pages = SystemTypeDetail.dao.paginate(page, limit, "select *", sql, type_code);
        List datalist = pages.getList();
//        for (int k = 0; k < datalist.size(); k++) {
//            SystemRole st = (SystemRole) datalist.get(k);
//        }
        LayuiData ld = LayuiData.newData();
        ld.setCount(pages.getTotalRow());
        ld.setData(datalist);
        renderJson(ld);
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void ModifySystemTypeDataApi() {
        Map datas = JsonUtil.json2Map(getPara("postdata"));
        String type_code = StringUtil.getString(datas, "type_code");
        String actions = StringUtil.getString(datas, "actions");
        if ("EDIT".equals(actions)) {
            boolean results = SysType.dao.findById(type_code)
                    .set("type_name", StringUtil.getString(datas, "type_name"))
                    .set("type_desc", StringUtil.getString(datas, "type_desc"))
                    .set("sindex", StringUtil.getInt(datas, "sindex"))
                    .update();
            if (results) {
                renderJson(JsonResult.getResult(0, "修改成功"));
            } else {
                renderJson(JsonResult.getResult(9, "修改失败"));
            }//
        } else {
            boolean results = new SysType()
                    .set("type_code", StringUtil.getString(datas, "type_code"))
                    .set("type_name", StringUtil.getString(datas, "type_name"))
                    .set("type_desc", StringUtil.getString(datas, "type_desc"))
                    .set("sindex", StringUtil.getInt(datas, "sindex"))
                    .save();
            if (results) {
                renderJson(JsonResult.getResult(0, "添加成功"));
            } else {
                renderJson(JsonResult.getResult(9, "添加失败"));
            }//
        }
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void CheckTypeCodeApi() {
        String type_code = getPara("type_code", "");
        renderJson(VerifyKey(SysType.dao, type_code, "分类CODE"));
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void DelSystemTypeApi() {
        String type_code = getPara("type_code", "");
        int counts = Db.queryInt("select count(*) from sys_type_detail where type_code=?", type_code);
        if (counts == 0) {
            if (SysType.dao.findById(type_code).delete()) {
                renderJson(JsonResult.getResult(0, "删除成功"));
            } else {
                renderJson(JsonResult.getResult(9, "删除失败"));
            }
        } else {
            renderJson(JsonResult.getResult(9, "该分类下有" + counts + "子分类,不能删除"));
        }
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void AddSystemTypeDetailApi() {
        String type_code = getPara("type_code", "");
        new SysTypeDetail().set("type_code", type_code)
                .set("sindex", 0)
                .set("d_name", "子分类")
                .set("d_desc", "分类说明")
                .save();
        renderJson(JsonResult.getResult(0, "添加成功"));
    }

    /**
     * 删除子分类API
     */
    @Admins(roles = "system", name = "删除子分类API", log = true)
    public void DelSystemTypeDetailApi() {
        int d_id = getParaToInt("d_id", 0);
        new SysTypeDetail().findById(d_id).delete();
        renderJson(JsonResult.getResult(0, "删除成功"));
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void ModifySystemTypeDetailFieldApi() {
        String d_id = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysTypeDetail.dao.findById(d_id).set(field, value).update();
        renderJson(JsonResult.getResult(0, "修改成功"));
    }

    /**
     * 修改主分类API
     */
    @Roles(roles = "system")
    public void ChangeSysTypeDetailApi() {
        String d_id = getParaStr("d_id", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysTypeDetail.dao, d_id, field, msg));
    }

    /**
     *
     */
    @Roles(roles = "system")
    public void InitSystemType() {
        SysTypeCache.reload();
        renderJson(JsonResult.getResult(0, "重置成功"));
    }

}